package com.example.demo_news.controller.user;

import javax.servlet.http.HttpServletRequest;

import com.example.demo_news.entity.News;
import com.example.demo_news.entity.OfficialAccount;
import com.example.demo_news.service.User.OfficialAccountService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

@Controller
//official角色才能访问
@PreAuthorize("hasAnyRole('official')")
public class OfficialAccountController {
    @Autowired
    private OfficialAccountService officialAccountService;
    @RequestMapping("/officialAccount")
    public String officialAccount(Model model ,Integer page){
        //返回当前账号的所有信息
        officialAccountService.findNewsByofficialAccountId(model,page);
        return "/user/officialAccount";
    }
    @RequestMapping("/addNews")
    public String add(News news ,HttpServletRequest request) {
        return "/user/addNews";                                                                         
    }
    //添加新闻
    @RequestMapping("/toAddNews")
    public String save(News news,HttpServletRequest request){
       if(officialAccountService.save(news, request)){
        return "/user/addNews";
       }       
        return "/user/officialAccount";
    }
    //新闻号完善信息
     @RequestMapping("/myOfficialMessage")
     public String myOfficialMessage(Model model){
         officialAccountService.myOfficialMessage(model);
         return "/user/myOfficialMessage";
     }
     //修改信息
     @RequestMapping("/modifyOfficialMessage")
     public String modifyOfficialMessage(OfficialAccount officialAccount,MultipartFile photo) throws Exception{
         officialAccountService.modifyOfficial(officialAccount, photo);
         return "/user/myOfficialMessage";
     }
    //详细信息
    @RequestMapping("/newsDetail")
    public String newsDetail(Integer id,Model model){
        officialAccountService.findNewsById(id, model);
        return "user/newsDetail";
    }

    //删除发布的新闻
    @RequestMapping("/deleteNewsDetail")
    public String deleteNewsDetail(Integer id){
        officialAccountService.deleteNewsDetail(id);
        return "/user/officialAccount";
    }
}
